﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.17
 * 描 述：设置执行时间
 */
var refreshGirdData; // 更新数据
var bootstrap = function ($, learun) {
    "use strict";

    var userids = learun.frameTab.currentIframe().needSetActionTimeUserids;

    // 保存数据
    var acceptClick = function () {// 0保存并新增 1保存

        var productData = [];
        var productDataTmp = $('#gridtable').jfGridGet('rowdatas');

        if (productDataTmp.length == 0) {
            learun.alert.error('表单信息不全,请检查！');
            return false;
        }
        for (var i = 0, l = productDataTmp.length; i < l; i++) {
            if (!!productDataTmp[i]['id']) {
                if (!productDataTmp[i].actionTime) {
                    learun.alert.error('请填写执行时间！');
                    return;
                }
                productData.push(productDataTmp[i]);
            }
        }

        learun.layerConfirm('注：您确认要保存此操作吗？', function (res, index) {
            if (res) {

                $.lrSaveForm(config.base_server() + 'users/saveHistorys?access_token=' + config.getToken().access_token, JSON.stringify(productData), function (res) {
                    if (res.resp_code == 200) {
                        // if (type == 0) {
                        //     window.location.href = config.webportal_server + 'pages/asset/apply/form.html?access_token=' + config.getToken().access_token
                        // } else {
                        //     learun.frameTab.close('apply_add');
                        // }
                    }
                });

                top.layer.close(index); //再执行关闭
            }
        });
    };

    var page = {
        init: function () {
            page.initGrid();
            page.bind();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 保存
            $('#lr_save').on('click', function () {
                acceptClick();
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'users/historys?access_token=' + config.getToken().access_token,
                headData: [
                    {label: "部门", name: "departmentName", width: 140, align: "left"},
                    {label: "姓名", name: "realName", width: 120, align: "left"},
                    {label: "原工资待遇", name: "levels", width: 160, align: "center"},
                    {label: "原实际职务", name: "job", width: 160, align: "center"},
                    {label: "新工资待遇", name: "levelsNew", width: 160, align: "center"},
                    {label: "新实际职务", name: "jobNew", width: 160, align: "center"},
                    {
                        label: "执行时间", name: "actionTime", width: 120, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd');
                        },
                        edit: {
                            type: 'datatime',
                            dateformat: '0',       // 0:yyyy-MM-dd;1:yyyy-MM-dd HH:mm,格式
                            init: function (data, $edit) {// 在点击单元格的时候触发，可以用来初始化输入控件，行数据和控件对象

                            },
                            change: function (data, num) {// 行数据和行号
                            }
                        }
                    },
                    {
                        label: "添加时间", name: "createTime", width: 140, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd');
                        }
                    },
                    {label: "id", name: "id", width: 1, align: "left", hidden: true}
                ],
                isEdit: true,
            });
            page.search();
        },
        search: function (param) {
            param = param || {};
            param.userids = userids;
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    }

    page.init();
}


